package cn.ctodb.cms.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.*;
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;

/**
 * A CmsContent.
 */
@Entity
@Table(name = "cms_content")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "cmscontent")
public class CmsContent implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "title")
    private String title;

    @Column(name = "tpl")
    private String tpl;

    @Column(name = "key_word")
    private String keyWord;

    @Column(name = "description")
    private String description;

    @Column(name = "author")
    private String author;

    @Column(name = "update_date")
    private ZonedDateTime updateDate;

    @Column(name = "view_count")
    private Integer viewCount;

    @Column(name = "comment_count")
    private Integer commentCount;

    @OneToMany(mappedBy = "cmsContent")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<CmsTxt> txts = new HashSet<>();

    @OneToMany(mappedBy = "cmsContent")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<CmsImg> imgs = new HashSet<>();

    @OneToMany(mappedBy = "cmsContent")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<CmsFile> files = new HashSet<>();

    @ManyToOne
    private CmsChannel channel;

    @ManyToMany
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @JoinTable(name = "cms_content_tag",
               joinColumns = @JoinColumn(name="cms_contents_id", referencedColumnName="ID"),
               inverseJoinColumns = @JoinColumn(name="tags_id", referencedColumnName="ID"))
    private Set<CmsTag> tags = new HashSet<>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public CmsContent title(String title) {
        this.title = title;
        return this;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTpl() {
        return tpl;
    }

    public CmsContent tpl(String tpl) {
        this.tpl = tpl;
        return this;
    }

    public void setTpl(String tpl) {
        this.tpl = tpl;
    }

    public String getKeyWord() {
        return keyWord;
    }

    public CmsContent keyWord(String keyWord) {
        this.keyWord = keyWord;
        return this;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public String getDescription() {
        return description;
    }

    public CmsContent description(String description) {
        this.description = description;
        return this;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getAuthor() {
        return author;
    }

    public CmsContent author(String author) {
        this.author = author;
        return this;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public ZonedDateTime getUpdateDate() {
        return updateDate;
    }

    public CmsContent updateDate(ZonedDateTime updateDate) {
        this.updateDate = updateDate;
        return this;
    }

    public void setUpdateDate(ZonedDateTime updateDate) {
        this.updateDate = updateDate;
    }

    public Integer getViewCount() {
        return viewCount;
    }

    public CmsContent viewCount(Integer viewCount) {
        this.viewCount = viewCount;
        return this;
    }

    public void setViewCount(Integer viewCount) {
        this.viewCount = viewCount;
    }

    public Integer getCommentCount() {
        return commentCount;
    }

    public CmsContent commentCount(Integer commentCount) {
        this.commentCount = commentCount;
        return this;
    }

    public void setCommentCount(Integer commentCount) {
        this.commentCount = commentCount;
    }

    public Set<CmsTxt> getTxts() {
        return txts;
    }

    public CmsContent txts(Set<CmsTxt> cmsTxts) {
        this.txts = cmsTxts;
        return this;
    }

    public CmsContent addTxt(CmsTxt cmsTxt) {
        txts.add(cmsTxt);
        cmsTxt.setCmsContent(this);
        return this;
    }

    public CmsContent removeTxt(CmsTxt cmsTxt) {
        txts.remove(cmsTxt);
        cmsTxt.setCmsContent(null);
        return this;
    }

    public void setTxts(Set<CmsTxt> cmsTxts) {
        this.txts = cmsTxts;
    }

    public Set<CmsImg> getImgs() {
        return imgs;
    }

    public CmsContent imgs(Set<CmsImg> cmsImgs) {
        this.imgs = cmsImgs;
        return this;
    }

    public CmsContent addImg(CmsImg cmsImg) {
        imgs.add(cmsImg);
        cmsImg.setCmsContent(this);
        return this;
    }

    public CmsContent removeImg(CmsImg cmsImg) {
        imgs.remove(cmsImg);
        cmsImg.setCmsContent(null);
        return this;
    }

    public void setImgs(Set<CmsImg> cmsImgs) {
        this.imgs = cmsImgs;
    }

    public Set<CmsFile> getFiles() {
        return files;
    }

    public CmsContent files(Set<CmsFile> cmsFiles) {
        this.files = cmsFiles;
        return this;
    }

    public CmsContent addFile(CmsFile cmsFile) {
        files.add(cmsFile);
        cmsFile.setCmsContent(this);
        return this;
    }

    public CmsContent removeFile(CmsFile cmsFile) {
        files.remove(cmsFile);
        cmsFile.setCmsContent(null);
        return this;
    }

    public void setFiles(Set<CmsFile> cmsFiles) {
        this.files = cmsFiles;
    }

    public CmsChannel getChannel() {
        return channel;
    }

    public CmsContent channel(CmsChannel cmsChannel) {
        this.channel = cmsChannel;
        return this;
    }

    public void setChannel(CmsChannel cmsChannel) {
        this.channel = cmsChannel;
    }

    public Set<CmsTag> getTags() {
        return tags;
    }

    public CmsContent tags(Set<CmsTag> cmsTags) {
        this.tags = cmsTags;
        return this;
    }

    public CmsContent addTag(CmsTag cmsTag) {
        tags.add(cmsTag);
        return this;
    }

    public CmsContent removeTag(CmsTag cmsTag) {
        tags.remove(cmsTag);
        return this;
    }

    public void setTags(Set<CmsTag> cmsTags) {
        this.tags = cmsTags;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        CmsContent cmsContent = (CmsContent) o;
        if (cmsContent.id == null || id == null) {
            return false;
        }
        return Objects.equals(id, cmsContent.id);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public String toString() {
        return "CmsContent{" +
            "id=" + id +
            ", title='" + title + "'" +
            ", tpl='" + tpl + "'" +
            ", keyWord='" + keyWord + "'" +
            ", description='" + description + "'" +
            ", author='" + author + "'" +
            ", updateDate='" + updateDate + "'" +
            ", viewCount='" + viewCount + "'" +
            ", commentCount='" + commentCount + "'" +
            '}';
    }
}
